package com.blog.backend.controller;

import com.blog.backend.annotation.LoginAnno;
import com.blog.backend.config.FlagConfig;
import com.blog.backend.resp.PictureUrlResp;
import com.blog.backend.resp.WangEditor;
import com.blog.backend.service.FileService;
import com.blog.backend.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fanggs
 * @Date 2021/7/16 0016 11:32
 */

@RestController
@Api(tags = "文件处理接口")
public class FileController {

    @Resource
    private FileService fileService;

    @PostMapping("/file/avatar")
    @ApiOperation("头像图片文件上传")
    @LoginAnno
    public Result<PictureUrlResp> fileUpload(MultipartFile file){
        PictureUrlResp resp = fileService.uploadFile(file, FlagConfig.Avatar.getCode());
        return Result.success(resp);
    }

    @PostMapping("/file/photoAlbum")
    @ApiOperation("相册图片文件上传")
    @LoginAnno
    public Result<PictureUrlResp> photoAlbum(MultipartFile file){
        PictureUrlResp resp = fileService.uploadFile(file, FlagConfig.PHOTO_ALBUM.getCode());
        return Result.success(resp);
    }

    @PostMapping("/file/richText")
    @ApiOperation("富文本图片上传")
    @LoginAnno
    public Result<PictureUrlResp> richTextPic(MultipartFile file){
        PictureUrlResp resp=fileService.uploadFile(file,FlagConfig.RICH_TEXT.getCode());
        return  Result.success(resp);
    }

    @PostMapping("/file/wangeditor")
    @ApiOperation("富文本图片文件上传")
    public  Result<PictureUrlResp> wangeditor(MultipartFile file){
        PictureUrlResp resp = fileService.uploadFile(file, FlagConfig.PHOTO_ALBUM.getCode());
//        Map<String, Object> map = new HashMap<>();
//        map.put("errno",0);
//        WangEditor wangEditor = new WangEditor();
        resp.setPictureId("https://lmg.jj20.com/up/allimg/1113/050920095302/20050Z95302-18-1200.jpg");
        resp.setPictureUrl("https://lmg.jj20.com/up/allimg/1113/050920095302/20050Z95302-18-1200.jpg");
//        wangEditor.setAlt("图片名称");
//        WangEditor[] array=new WangEditor[1];
//        array[0]=wangEditor;
//        map.put("data",array);
        return Result.success(resp);
    }


    @GetMapping("/file/deleteFile")
    @LoginAnno
    @ApiOperation("删除文件")
    public Result<Boolean> deleteFile(String fileId){
        Boolean result=fileService.deleteFile(fileId);
        return Result.success(result);
    }
}
